<!--意见反馈管理-->
<template>
  <div id="upmsFeedback" class="app-container">
    <c-list
      :data-source="list"
      empty="暂无意见反馈"
      item-layout="vertical"
      :hover="false"
      padding="0 10px"
      class="msg-list-container">
      <template #header>
        <table-toolbar :remove="false" @create="handleCreate" @refresh="handleFilter">
          <template slot="rightTool">
            <search v-model="listQuery.keyword" placeholder="请输入意见反馈标题" @search="handleFilter" />
          </template>
        </table-toolbar>
      </template>
      <template #item="{item}">
        <c-item :title-noraml="true">
          <template #titleTag>
            <el-tag v-if="item.isReply===1" size="mini" effect="plain">已回复</el-tag>
            <el-tag v-if="item.isReply===0" type="danger" size="mini" effect="plain"> 未回复</el-tag>
          </template>
          <template #title>
            <span class="common-cursor" @click="handleLookDetails(item)">{{ item.title }}</span>
          </template>
          <template #actions>
            <div class="color-default" style="font-size: 12px;flex: 1;">
              {{ item._category }}&nbsp;&nbsp;&nbsp;发布于.{{ item.gmtCreated | formatDate }}
            </div>
            <div style="font-size: 12px;">
              <i class="el-icon-delete color-danger common-cursor" @click="handleRemove(item)" />
            </div>

          </template>
          <template>
            <div class="color-default" style="font-size: 12px;"> {{ item.content }} </div>
          </template>
        </c-item>
      </template>
      <template v-show="total>0" #pagination>
        <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchData" />
      </template>
    </c-list>
    <!--新增/编辑侧边栏-->
    <side-box :show="sideBox.recordShow" :title="sideBox.title" @close="sideBox.recordShow = !sideBox.recordShow">
      <template slot="body">
        <el-form
          ref="dataForm"
          :rules="upmsFeedbackRules"
          :model="upmsFeedbackRecord"
          label-position="right"
          label-width="120px">
          <el-form-item label="标题:" prop="title">
            <el-input v-model="upmsFeedbackRecord.title" maxlength="40" show-word-limit placeholder="请输入" />
          </el-form-item>
          <el-form-item label="分类:" prop="category">
            <category-dict-select v-model="upmsFeedbackRecord.category" type="04" />
          </el-form-item>

          <el-form-item label="意见反馈:" prop="content">
            <el-input
              v-model="upmsFeedbackRecord.content"
              :rows="4"
              type="textarea"
              maxlength="1000"
              show-word-limit
              placeholder="请输入" />
          </el-form-item>
          <el-form-item label="图片:">
            <MultipleImageUpload
              v-model="upmsFeedbackRecord.images"
              :catalog="constants.FILE_CATALOG_FEEDBACK"
              :limit="5"
              :crop="false"
              desc="单张照片不能超过2M，最多只允许上传5张照片" />
          </el-form-item>
          <el-form-item label="小视频:">
            <SimpleUploader
              v-model="upmsFeedbackRecord.tempVideos"
              text="上传视频"
              accept="audio/mp4, video/mp4"
              :catalog="constants.FILE_CATALOG_FEEDBACK" />
          </el-form-item>
        </el-form>
      </template>
      <template slot="foot">
        <el-button type="primary" plain @click="sideBox.status==='create'?createData():updateData()">保存 </el-button>
      </template>
    </side-box>
    <model-dialog
      :title="'【'+upmsFeedbackRecord.title+'】详情'"
      :visible.sync="detailsDialogVisible"
      width="600px"
      @close="detailsDialogVisible=!detailsDialogVisible">
      <el-form
        ref="dataForm"
        :model="upmsFeedbackRecord"
        label-width="120px">
        <el-form-item label="标题:">
          <el-tag>{{ upmsFeedbackRecord._category }}</el-tag>
          {{ upmsFeedbackRecord.title }}
        </el-form-item>
        <el-form-item label="反馈人:" prop="createdBy">
          {{ upmsFeedbackRecord.createdBy | optionUserFormat }}
        </el-form-item>
        <el-form-item label="描述:">
          <span class="color-danger">{{ upmsFeedbackRecord.content }}</span>
        </el-form-item>
        <el-form-item v-if="upmsFeedbackRecord.images" label="图片:">
          <FileItem :files="upmsFeedbackRecord.images.split(',')" />
        </el-form-item>
        <el-form-item v-if="upmsFeedbackRecord.videos" label="小视频:">
          <FileItem :files="upmsFeedbackRecord.videos.split(',')" />
        </el-form-item>
        <el-form-item label="回复:">
          <span v-if="upmsFeedbackRecord.isReply=== 0 " class="color-danger ">未回复</span>
          <span v-else class="color-primary">{{ upmsFeedbackRecord.replyContent }} </span>
        </el-form-item>
        <template v-if="upmsFeedbackRecord.isReply===1 ">
          <el-form-item label="回复时间:">
            {{ upmsFeedbackRecord.replyPeople | optionUserFormat }}
          </el-form-item>
          <el-form-item label="回复时间:">
            {{ upmsFeedbackRecord.replyTime | formatDate }}
          </el-form-item>
        </template>

      </el-form>
    </model-dialog>
  </div>
</template>

<script>
export default {
  components: {},

  data() {
    return {
      list: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: this.common.PAGE_LIMIT_DEFAULT,
        keyword: undefined,
        sort: this.common.SORT_DEFAULT
      },

      // 固定写法
      sideBox: {
        recordShow: false,
        title: '',
        status: 'create'
      },
      upmsFeedbackRecord: {},
      upmsFeedbackRules: {
        createdBy: [
          { required: true, message: '请输入创建者', trigger: 'blur' }
        ],
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        category: [
          { required: true, message: '请输入分类', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      },
      detailsDialogVisible: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    /* 搜索*/
    handleFilter() {
      this.listQuery = {
        ...this.listQuery,
        page: 1
      }
      this.fetchData()
    },
    /**
     * 重置
     */
    handleReset() {
      this.listQuery = {
        ...this.listQuery,
        page: 1,
        limit: this.common.PAGE_LIMIT_DEFAULT,
        keyword: undefined,
        sort: this.common.SORT_DEFAULT
      }
      this.fetchData()
    },
    async fetchData() {
      const response = await this.$store.dispatch('currentUserFeedbackPage', this.listQuery)
      this.list = response.records
      this.total = response.total
    },
    /* 新增*/
    handleCreate() {
      this.upmsFeedbackRecord = {
        tempVideos: []
      }
      this.sideBox.status = 'create'
      this.sideBox.recordShow = true
      this.sideBox.title = '新增意见反馈'
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          var temp = Object.assign({}, this.upmsFeedbackRecord)
          temp = {
            ...temp,
            videos: temp.tempVideos.join(',')
          }
          delete temp.tempVideos
          this.$store.dispatch('upmsFeedbackSave', temp).then(response => {
            this.sideBox.recordShow = false
            this.$modal.msgSuccess('创建成功')
            this.handleReset()
          }).catch(() => {
            this.sideBox.recordShow = true
          })
        }
      })
    },
    /* 编辑*/
    handleUpdate(row) {
      this.upmsFeedbackRecord = Object.assign({}, row) // copy obj
      this.upmsFeedbackRecord = {
        ... this.upmsFeedbackRecord,
        tempVideos: this.upmsFeedbackRecord.videos ? this.upmsFeedbackRecord.videos.split(',') : []
      }
      this.sideBox.status = 'update'
      this.sideBox.recordShow = true
      this.sideBox.title = '编辑意见反馈'
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          var tempData = Object.assign({}, this.upmsFeedbackRecord)
          tempData = {
            ...tempData,
            videos: tempData.tempVideos.join(',')
          }
          delete tempData.tempVideos
          this.$store.dispatch('upmsFeedbackUpdate', tempData).then(response => {
            this.sideBox.recordShow = false
            this.$modal.msgSuccess('修改成功')
            this.handleFilter()
          }).catch(() => {
            this.sideBox.recordShow = true
          })
        }
      })
    },

    /* 删除*/
    handleRemove(item) {
      this.$confirm('此操作将永久删除记录, 是否继续?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('upmsFeedbackRemove', [item.id]).then(response => {
          this.handleReset()
          this.$modal.msgSuccess('删除成功')
        }).catch(() => {})
      }).catch(() => { })
    },
    handleLookDetails(row) {
      this.upmsFeedbackRecord = Object.assign({}, row) // copy obj
      this.detailsDialogVisible = true
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
